package com.jy.rhin.application.registration;

import cn.hutool.core.collection.CollUtil;
import cn.org.chiss.rhin._2015.registration.terminology.ConceptMapQueryRequest;
import cn.org.chiss.rhin._2015.registration.terminology.ConceptMapQueryResponse;
import cn.org.chiss.rhin._2015.registration.terminology.RetrieveConceptMapResponse;
import com.jy.api.misc.MiscAPI;
import com.jy.api.misc.dto.ISTTR4QueryDto;
import com.jy.api.misc.dto.ZhiYuYSMXDto;
import com.jy.rhin.application.assembler.ConceptMapAssembler;
import com.jy.rhin.config.RhinFaultSubCode;
import com.jy.rhin.facade.ws.basic.wsEntry.exception.RhinWSException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ConceptMapService {
    @Resource
    private MiscAPI miscAPI;

    public RetrieveConceptMapResponse jiZhiYSHQ(String id) {
        RetrieveConceptMapResponse response = new RetrieveConceptMapResponse();

        ZhiYuYSMXDto dto =  miscAPI.jiZhiYSHQ(id).getData();
        if (dto == null) {
            throw new RhinWSException(RhinFaultSubCode.ConceptMapNotFoundFault);
        }
        response.setConceptMap(ConceptMapAssembler.toConceptMap(dto));
        return response;
    }

    public ConceptMapQueryResponse jiZhiYSCX(ConceptMapQueryRequest query) {
        ConceptMapQueryResponse response = new ConceptMapQueryResponse();
        ISTTR4QueryDto reqDto = new ISTTR4QueryDto();
        reqDto.setDisplayNameContain(query.getQueryPayload().getDisplayNameContain().getValue());
        List<ZhiYuYSMXDto> list = miscAPI.jiZhiYSCX(reqDto).getData();
        if (CollUtil.isEmpty(list)) {
            throw new RhinWSException(RhinFaultSubCode.ConceptMapNotFoundFault);
        }
        response.setConceptMap(list.stream().map(ConceptMapAssembler::toConceptMap).collect(Collectors.toList()));
        return response;
    }
}
